/**
 * Copyright (C) 2025 pony working team
 * SPDX-License-Identifier: MulanPSL-2.0
 */

/* includes (local) ----------------------------------------------------------*/
#include "m_heap.h"
/* includes (standard library, system) ---------------------------------------*/
#include <stdlib.h>
/* includes (other library) --------------------------------------------------*/
/* includes (project) --------------------------------------------------------*/

/* defines -------------------------------------------------------------------*/
/* typedefs ------------------------------------------------------------------*/
/* macro ---------------------------------------------------------------------*/
/* variables (global) --------------------------------------------------------*/
/* variables (extern) --------------------------------------------------------*/
/* variables (local) ---------------------------------------------------------*/
/* inlines -------------------------------------------------------------------*/
/* functions -----------------------------------------------------------------*/

void *malloc(size_t size)
{
    return heap_alloc(kernel_heap, size);
}

void *calloc(size_t size, size_t len)
{
    size_t s = size * len;
    void *ptr;

    if (s < size || s < len) {
        return NULL;
    }

    ptr = malloc(s);

    if (ptr != NULL) {
        memset(ptr, 0, s);
    }

    return ptr;
}

void *realloc(void *ptr, size_t size)
{
    return (void *)heap_realloc(kernel_heap, (char *)ptr, size);
}

void free(void *ptr)
{
    heap_free(kernel_heap, ptr);
}

void *memalign(size_t alignment, size_t size)
{
    return heap_alloc_align(kernel_heap, alignment, size);
}

#ifdef __NEWLIB__

void *_malloc_r(struct _reent *ptr, size_t size)
{
    return malloc(size);
}

void *_realloc_r(struct _reent *ptr, void *old, size_t newlen)
{
    return realloc(old, newlen);
}

void *_calloc_r(struct _reent *ptr, size_t size, size_t len)
{
    return calloc(size, len);
}

void _free_r(struct _reent *ptr, void *addr)
{
    free(addr);
}

#endif
